package com.abe.bathsystem.widget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;

import com.abe.bathsystem.adapter.AbeCommonAdapter;
import com.abe.bathsystem.adapter.AbeMultiItemTypeAdapter;
import com.abe.bathsystem.adapter.AbeViewHolder;
import com.abe.bathsystem.widget.OnOperateListener;
import com.abe.bathsystem.widget.recyclerview.DefaultItemDecoration;
import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.ToastUtils;

import net.yet.campusdev.R;

import java.util.ArrayList;
import java.util.List;

public class ChooseLogDialog extends BaseDialog {

    private List<String> files;
    private DialogContract.OnChooseFileListener listener;

    public ChooseLogDialog(@NonNull Context context, OnOperateListener operateListener, List<String> files) {
        super(context, operateListener);
        this.files = files;
    }

    public ChooseLogDialog(@NonNull Context context, List<String> files) {
        super(context);
        this.files = files;
    }

    public void setListener(DialogContract.OnChooseFileListener listener) {
        this.listener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item_view_user_choose_bathpos);
        initRV();
    }

    private void initRV() {
        RecyclerView itemRv = findViewById(R.id.item_rv);
        if (files == null) files = new ArrayList<>();
        AbeCommonAdapter<String> adapter = new AbeCommonAdapter<String>(getContext(), R.layout.item_view_v3_log, files) {
            @Override
            protected void convert(AbeViewHolder holder, String fileName, int position) {
                int colorRes = position % 2 == 0 ? R.color.cbd : R.color.white;
                holder.setBackgroundColor(R.id.item_file_name, getContext().getResources().getColor(colorRes));
                holder.setText(R.id.item_file_name, fileName);
            }
        };
        adapter.setOnItemClickListener(new AbeMultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (listener != null) {
                    listener.onChooseLog(files.get(position));
                    CommonUtils.w("选中文件名：" + files.get(position));
                } else {
                    ToastUtils.show("未设置回调监听");
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        itemRv.setLayoutManager(new LinearLayoutManager(getContext()));
        itemRv.addItemDecoration(new DefaultItemDecoration(CommonUtils.getColor(R.color.transparent)));
        itemRv.setAdapter(adapter);
    }
}
